<template>
  <a-popover
    v-model="visible"
    trigger="click"
    placement="bottomRight"
    overlayClassName="header-notice-wrapper"
    :getPopupContainer="() => $refs.noticeRef.parentElement"
    :autoAdjustOverflow="true"
    :arrowPointAtCenter="true"
    :overlayStyle="{ width: '300px', top: '50px' }"
  >
    <template slot="content">
      <a-spin :spinning="loading">
        <a-tabs>
          <a-tab-pane key="1">
            <span slot="tab">
              通知
              <a-badge dot/>
            </span>
            <a-list :dataSource="messages">
              <a-list-item slot="renderItem" slot-scope="item">
                <a-list-item>
                  <a-list-item-meta :description="item.description">
                    <a slot="title" :href="item.href">{{ item.title }}</a>
                    <a-avatar style="background-color: white" slot="avatar" :src="item.avatar"/>
                  </a-list-item-meta>
                </a-list-item>
              </a-list-item>
              <!-- <a-list-item>
                <a-list-item-meta @click="showClick" title="你收到了 14 份新周报" description="一年前">
                  <a slot="title"></a>
                  <a-avatar style="background-color: white" slot="avatar" src="https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png"/>
                </a-list-item-meta>
              </a-list-item> -->
            </a-list>
          </a-tab-pane>
          <a-tab-pane tab="消息" key="2">
            123
          </a-tab-pane>
          <a-tab-pane tab="待办" key="3">
            123
          </a-tab-pane>
        </a-tabs>
      </a-spin>
    </template>
    <span @click="fetchNotice" class="header-notice" ref="noticeRef" style="padding: 0 18px">
      <a-badge :count="count">
        <a-icon style="font-size: 16px; padding: 4px" type="bell" />
      </a-badge>
    </span>
  </a-popover>
</template>

<script>
import { getNotice, getCounts, readAll } from '@/api/notice'

const messages = []

export default {
  name: 'HeaderNotice',
  data () {
    return {
      count: messages.length,
      loading: false,
      visible: false,
      id: null,
      socket: null,
      messages
    }
  },
  methods: {
    fetchNotice () {
      if (!this.visible) {
        this.loading = true
        getNotice(this.id).then(res => {
          console.log('res', res)
          this.messages = res.result
          this.loading = false
          readAll(this.id).then(res => {
            this.count = 0
          })
        }).catch(res => {
          this.loading = false
          this.count = 0
        })
      } else {
        this.loading = false
      }
      this.visible = !this.visible
    }
  },
  created () {
    if (typeof (WebSocket) !== 'undefined') {
      // 实例化socket
      this.socket = new WebSocket(this.path)
      // 监听socket连接
      this.socket.onopen = this.open
      // 监听socket错误信息
      this.socket.onerror = this.error
      // 监听socket消息
      this.socket.onmessage = this.getMessage
    }
    this.id = this.$store.getters.userInfo.id
    getCounts(this.id).then(res => {
      this.count = res.result
    })
  },
  destroyed () {
    // 销毁监听
    this.socket.onclose = this.close
  }
}
</script>

<style lang="css">
  .header-notice-wrapper {
    top: 50px !important;
  }
</style>
<style lang="less" scoped>
  .header-notice{
    display: inline-block;
    transition: all 0.3s;

    span {
      vertical-align: initial;
    }
  }
</style>
